/**
 * Copyright (c) 2019 EOVA.CN. All rights reserved.
 * 
 * Licensed under the EPPL license: http://eova.cn/eppl.txt
 * For authorization, please contact: admin@eova.cn
 */
package com.oss.hotel;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.time.DateUtil;
import com.eova.common.utils.xx;
import com.jfinal.kit.LogKit;
import com.oss.model.Area;
import com.util.HttpUtil;
import com.util.JsonUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.Date;
import java.util.Map;

public class HotelIntercept extends MetaObjectIntercept {

	@Override
	public void queryBefore(AopContext ac) throws Exception {

		String corporation = ac.user.getStr("corporation");
		String department = ac.user.getStr("department");
		String company = ac.user.getStr("company");
		int hotel_id=ac.user.get("hotel_id");

		if(!xx.isEmpty(corporation)){
			ac.condition = " and corporation = '" + corporation+"' ";
		}
		if(!xx.isEmpty(company)){
			ac.condition = " and company = '" + company+"' ";
		}
		if(!xx.isEmpty(department)){
			ac.condition = " and department = '" + department+"' ";
		}
		if(0!=hotel_id){
			ac.condition = " and id = " + hotel_id;
		}
	}

	public String addBefore(AopContext ac) throws Exception {

		ac.record.set("add_time", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		String pro= Area.dao.getNameById(ac.record.get("province")+"");
		String city= Area.dao.getNameById(ac.record.get("city")+"");
		String region= Area.dao.getNameById(ac.record.get("region")+"");
		String address = pro + city + region + ac.record.get("address")+"";

		String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address.trim()
				+ "&output=json&ak=m07L7mGBXlGCG3NVxlOYKCdsxl7o3swL";

		HttpResponse response = HttpUtil.httpGet(url,null,0,"");
		String res = EntityUtils.toString(response.getEntity(), "UTF-8");
		Map retMap = JsonUtil.getMap4Json(res);
		String status = retMap.get("status").toString();
		String result = retMap.get("result").toString();
		if("0".equalsIgnoreCase(status)){
			LogKit.info(result);
			Map resultMap = JsonUtil.getMap4Json(result);
			String location = resultMap.get("location")+"";
			Map locaTionMap = JsonUtil.getMap4Json(location);
			String lng = locaTionMap.get("lng")+"";
			String lat = locaTionMap.get("lat")+"";
			LogKit.info("lng = "+lng+ " lat = "+lat);
			ac.record.set("point_x",lng).set("point_y",lat);
		}else{
			return Easy.warn("请输入正确地址信息!");
		}
		return super.addBefore(ac);
	}

	/**
	 * 更新前置 自动设置更新时间(将update_time字段设为禁止修改，由此提供数据)
	 */
	@Override
	public String updateBefore(AopContext ac) throws Exception {

		ac.record.set("modify_time", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		String pro= Area.dao.getNameById(ac.record.get("province")+"");
		String city= Area.dao.getNameById(ac.record.get("city")+"");
		String region= Area.dao.getNameById(ac.record.get("region")+"");
		String address = pro + city + region + ac.record.get("address")+"";

		String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address.trim()
				+ "&output=json&ak=m07L7mGBXlGCG3NVxlOYKCdsxl7o3swL";

		HttpResponse response = HttpUtil.httpGet(url,null,0,"");
		String res = EntityUtils.toString(response.getEntity(), "UTF-8");
		Map retMap = JsonUtil.getMap4Json(res);
		String status = retMap.get("status").toString();
		String result = retMap.get("result").toString();
		if("0".equalsIgnoreCase(status)){
			LogKit.info(result);
			Map resultMap = JsonUtil.getMap4Json(result);
			String location = resultMap.get("location")+"";
			Map locaTionMap = JsonUtil.getMap4Json(location);
			String lng = locaTionMap.get("lng")+"";
			String lat = locaTionMap.get("lat")+"";
			LogKit.info("lng = "+lng+ " lat = "+lat);
			ac.record.set("point_x",lng).set("point_y",lat);
		}else{
			return Easy.warn("请输入正确地址信息!");
		}
		return super.updateBefore(ac);
	}

}